/*
 *    Description:  dropbear server management
 *          Author:  dimmalex (dim), dimmalex@gmail.com
 *      Company:  HP
 */

#include "land/farm.h"



const char *_intro( obj_t this )
{
    const char *msg = \
"{"\
"\n    \"setup\":\"setup the dropbear\","\
"\n    \"shut\":\"shutdown the dropbear\","\
"\n    \"reset\":\"restart the dropbear\""\
"\n}\n";
    return msg;
}
talk_t _setup( obj_t this, param_t param )
{
    talk_t cfg;
    const char *ptr;
    const char *port;
    const char *test_dropbear = "/tmp/.dropbear_exsit";

    /* test the dropbear have */
    shell( "which dropbear > %s", test_dropbear );
    ptr = read_string( test_dropbear, NULL, 0 );
    if ( ptr == NULL || strlen( ptr ) < 8 )
    {
        shell( "rm %s", test_dropbear );
        return tfalse;
    }
    shell( "rm %s", test_dropbear );
    /* get the configure */
    cfg = config_sget( COM_IDPATH, NULL );
    /* get the status */
    ptr = json_get_string( cfg, "status" );
    if ( ptr == NULL || 0 != strcmp( ptr, "enable" ) )
    {
        talk_free( cfg );
        return tfalse;
    }
    /* get the only one manager */
    ptr = json_get_string( cfg, "manager" );
    if ( ptr != NULL && *ptr != '\0' )
    {
        struct in_addr iptest;
        port = json_get_string( cfg, "port" );
        /* lock it */
        debug( "shell( iptables -t filter -N %s_%s )", PROJECT_ID, COM_ID );
        debug( "shell( iptables -t filter -A INPUT -j %s_%s )", PROJECT_ID, COM_ID );
        shell( "iptables -t filter -N %s_%s >/dev/null 2>&1", PROJECT_ID, COM_ID );
        shell( "iptables -t filter -A INPUT -j  %s_%s", PROJECT_ID, COM_ID );
        if ( inet_pton( AF_INET, ptr, &iptest ) == 1 )
        {
            debug( "shell( iptables -A %s_%s -p tcp --dport %s  ! -s %s -j DROP )", PROJECT_ID, COM_ID, port, ptr );
            shell( "iptables -A %s_%s -p tcp --dport %s  ! -s %s -j DROP", PROJECT_ID, COM_ID, port, ptr );
        }
        else
        {
            debug( "shell( iptables -A %s_%s -p tcp --dport %s -m mac ! --mac-source %s -j DROP )", PROJECT_ID, COM_ID, port, ptr );
            shell( "iptables -A %s_%s -p tcp --dport %s -m mac ! --mac-source %s -j DROP", PROJECT_ID, COM_ID, port, ptr );
        }
    }
    /* start the service */
    serv_start( this, "service", NULL, COM_IDPATH );

    talk_free( cfg );
    return ttrue;
}
talk_t _shut( obj_t this, param_t param )
{
    debug( "shell( iptables -t filter -F %s_%s )", PROJECT_ID, COM_ID );
    debug( "shell( iptables -t filter -D INPUT -j %s_%s )",  PROJECT_ID, COM_ID );
    debug( "shell( iptables -t filter -X %s_%s )", PROJECT_ID, COM_ID );
    shell( "iptables -t filter -F %s_%s >/dev/null 2>&1", PROJECT_ID, COM_ID );
    shell( "iptables -t filter -D INPUT -j  %s_%s >/dev/null 2>&1", PROJECT_ID, COM_ID );
    shell( "iptables -t filter -X %s_%s >/dev/null 2>&1", PROJECT_ID, COM_ID );
    serv_stop( COM_IDPATH );
    return ttrue;
}
talk_t _reset( obj_t this, param_t param )
{
    serv_reset( COM_IDPATH );
    return ttrue;
}
talk_t _service( obj_t this, param_t param )
{
    int i;
    talk_t cfg;
    const char *port;
    const char *remote;
    char lanip[IDENTIFY_SIZE];
    char addr[IDENTIFY_SIZE];

    cfg = config_sget( COM_IDPATH, NULL );
    if ( cfg == NULL )
    {
        return tfalse;
    }
    /* get the port */
    port = json_get_string( cfg, "port" );
    if ( port == NULL || *port == '\0' )
    {
        port = "22";
    }
    /* get the remote allow */
    remote = json_get_string( cfg, "remote" );
    if ( remote != NULL && 0 == strcmp( remote, "enable" ) )
    {
        debug( "dropbear -F -p %s", port );
        execlp( "dropbear", "dropbear", "-F", "-p", port, "-K", "300", (char*)0 );
    }
    else
    {
        addr[0] = '\0';
        /* wait the lan up */
        for ( i=0; i<5; i++ )
        {
            if ( netcard_info( "lan", lanip, sizeof(lanip), NULL, 0, NULL, 0, NULL, 0 ) == 0 )
            {
                snprintf( addr, sizeof(addr), "%s:%s", lanip, port );
                break;
            }
            sleep( 2 );
        }
        if ( addr[0] == '\0' )
        {
            debug( "dropbear -F -p %s", port );
            execlp( "dropbear", "dropbear", "-F", "-p", port, "-K", "300", (char*)0 );
        }
        else
        {
            debug( "dropbear -F -p %s", addr);
            execlp( "dropbear", "dropbear", "-F", "-p", addr, "-K", "300", (char*)0 );
        }
    }

    talk_free( cfg );
    /* execl */
    faulting( "exec the %s error", "dropbear" );
    return tfalse;
}



boole _set( obj_t this, path_t path, talk_t v )
{
    obj_t o;
    boole ret;

    o = obj_create( COM_IDPATH );
    ret = config_set( o, path, v );
    if ( ret == true )
    {
        _shut( this, NULL );
        _setup( this, NULL );
        com_scall( "forward@firewall", "setup", NULL );
    }
    obj_free( o );
    return ret;
}
talk_t _get( obj_t this, path_t path )
{
    obj_t o;
    talk_t ret;

    o = obj_create( COM_IDPATH );
    ret = config_get( o, path );
    obj_free( o );
    return ret;
}



